package com.gome.axwl.aoc.web.controller.contract;

import com.gome.axwl.aoc.common.util.ErrorCode;
import com.gome.axwl.aoc.entity.dto.PageResultDto;
import com.gome.axwl.aoc.entity.exception.ZQException;
import com.gome.axwl.aoc.entity.po.ContractPo;
import com.gome.axwl.aoc.entity.po.EmptyContractResponse;
import com.gome.axwl.aoc.facade.ContractFacade;
import com.gome.axwl.aoc.facade.util.DownloadFileUtil;
import com.gome.common.utils.StringUtil;
import com.gome.commons.core.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * Created by wanglongyu on 2018/7/13.
 */
@Api(tags = "合同")
@RestController
@RequestMapping(value = "/contract")
public class ContractController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ContractFacade contractFacade;

    @ApiOperation(value = "分页查询合同信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractName",    value = "合同名称", paramType = "query",dataType="String"),
            @ApiImplicitParam(name = "carrierName",    value = "承运商名称", paramType = "query",dataType="String"),
            @ApiImplicitParam(name = "contractNo",      value = "合同编码", paramType = "query",dataType="String"),
            @ApiImplicitParam(name = "status",          value = "状态 1-已生效 2-未生效 不传表示全部", paramType = "query",dataType="int"),
            @ApiImplicitParam(name = "startTime",       value = "开始时间", paramType = "query",dataType="Date"),
            @ApiImplicitParam(name = "endTime",         value = "结束时间", paramType = "query",dataType="Date"),
            @ApiImplicitParam(name = "pageNum",         value = "当前页", paramType = "query",dataType="int"),
            @ApiImplicitParam(name = "pageSize",        value = "当前页条数", paramType = "query",dataType="int")
    })
    @RequestMapping(value = "/queryContract",method = RequestMethod.GET)
    public ResultData<PageResultDto<ContractPo>> queryContract(String contractName, String contractNo,String carrierName,Integer status, Date startTime,
                                                                       Date endTime, Integer pageNum, Integer pageSize){
        return ResultData.newResultData(contractFacade.queryContract(contractName, contractNo,null,carrierName,status, startTime, endTime, pageNum, pageSize));
    }

    @ApiOperation(value = "获取合同详情图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractNo", value = "合同编码", paramType = "query",dataType="String")
    })
    @RequestMapping(value = "/getDetail",method = RequestMethod.GET)
    public ResultData<List<String>> getDetail(String contractNo){
        if(StringUtil.isBlank(contractNo)){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL,ErrorCode.PARAM_IS_NULL_MSG);
        }
        try {
            return ResultData.newResultData(contractFacade.getDetail(contractNo));
        } catch (ZQException e) {
            logger.debug("获取合同详情图片失败，失败原因[ {} ]",e.getMessage());
            return ResultData.newResultData(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "下载合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractNo", value = "合同编码", paramType = "query",dataType="String"),
            @ApiImplicitParam(name = "contractName", value = "合同名称", paramType = "query",dataType="String"),
    })
    @RequestMapping(value = "/download",method = RequestMethod.GET)
    public ResultData<Integer> download(String contractNo,String contractName,HttpServletResponse response){
        if(StringUtil.isBlank(contractNo)){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL,ErrorCode.PARAM_IS_NULL_MSG);
        }
        String url = "";
        try {
            url =contractFacade.download(contractNo);
        } catch (ZQException e) {
            logger.debug("下载合同失败，失败原因[ {} ]",e.getMessage());
            return ResultData.newResultData(e.getCode(), e.getMessage());
        }
        InputStream inputStream = DownloadFileUtil.getFile(url);

        DownloadFileUtil.setResponseHeader(response,contractName+".pdf");
        OutputStream os;
        try {
            //获取自己的输入流数组
            byte[] getData = DownloadFileUtil.readInputStream(inputStream);
            os = response.getOutputStream();
            os.write(getData);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return ResultData.newSuccessResultData();
    }

    @ApiOperation(value = "提醒服务商签署合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractPhone", value = "服务商电话", paramType = "form",dataType="String"),
            @ApiImplicitParam(name = "carrierName",   value = "服务商名称", paramType = "form",dataType="String")
    })
    @RequestMapping(value = "/remindCarrier",method = RequestMethod.POST)
    public ResultData<Integer> remindCarrier(String contractPhone,String carrierName){
        if(StringUtil.isBlank(contractPhone)|| StringUtil.isBlank(carrierName)){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL,ErrorCode.PARAM_IS_NULL_MSG);
        }
        return contractFacade.remindCarrier(contractPhone,carrierName);
    }

    @ApiOperation(value = "获取空白合同详情图片等")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyInfoId", value = "申请单号id", paramType = "query",dataType="int")
    })
    @RequestMapping(value = "/getEmptyContract",method = RequestMethod.GET)
    public ResultData<EmptyContractResponse> getEmptyContract(Integer applyInfoId){
        if(applyInfoId == null){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL,ErrorCode.PARAM_IS_NULL_MSG);
        }
        try {
            return ResultData.newResultData(contractFacade.getEmptyContract(applyInfoId));
        } catch (ZQException e) {
            logger.debug("获取空白合同详情图片失败，失败原因[ {} ]",e.getMessage());
            return ResultData.newResultData(e.getCode(), e.getMessage());
        }
    }
    @ApiOperation(value = "运营平台签署合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyInfoId", value = "申请单号id", paramType = "form",dataType="int"),
            @ApiImplicitParam(name = "auditStatus", value = "审核状态 1-通过 2-未通过", paramType = "form",dataType="int"),
            @ApiImplicitParam(name = "signName",    value = "签署人姓名", paramType = "form",dataType="String"),
            @ApiImplicitParam(name = "unpassReason", value = "未通过原因", paramType = "form",dataType="String")
    })
    @RequestMapping(value = "/signByAoc",method = RequestMethod.POST)
    public ResultData<Integer> signByAoc(Integer applyInfoId,Integer auditStatus,String signName,String unpassReason){
        if(applyInfoId == null || auditStatus == null || StringUtil.isBlank(signName)){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL,ErrorCode.PARAM_IS_NULL_MSG);
        }
        try {
            return ResultData.newResultData(contractFacade.signByAoc(applyInfoId, auditStatus, signName, unpassReason));
        } catch (ZQException e) {
            logger.debug("获取空白合同详情图片失败，失败原因[ {} ]",e.getMessage());
            return ResultData.newResultData(e.getCode(), e.getMessage());
        }
    }
}
